package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.model.business.bo.adcampaignnamecustomer.AdTitleHistoryBo;
import com.ndp.fb.model.business.vo.cache.result.AdPublishInputHistoryCache;
import com.ndp.fb.model.business.vo.connectionobject.result.ConnectionObjectVo;
import com.ndp.fb.rdb.api.AdTitleHistoryRdbServices;
import com.ndp.fb.rdb.model.AdTitleHistory;
import com.ndp.fb.walle.business.AdCampNameCustomService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by infi on 2015/1/29.
 */
@Component
public class AdCampNameCustomServiceImpl implements AdCampNameCustomService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    private AdTitleHistoryRdbServices adTitleHistoryRdbServices;


    @Override
    public AdPublishInputHistoryCache buildAdPublishInputHistoryCache(Long creatorId, Long fbConnectionObjectId, String connectionObjectName) {
        AdPublishInputHistoryCache adPublishInputHistoryCache = new AdPublishInputHistoryCache();
        adPublishInputHistoryCache.setCreatorId(creatorId);
        adPublishInputHistoryCache.setConnectionObjectId(fbConnectionObjectId);
        adPublishInputHistoryCache.setConnectionObjectName(connectionObjectName);
        //找到当前创建者 当前产品下的 所有输入标题历史
        List<AdTitleHistory> adTitleHistoryList = adTitleHistoryRdbServices.findByCreatorIdAndConnectionObjectId(creatorId, fbConnectionObjectId);
        adPublishInputHistoryCache.setAdTitleHistoryList(tranferAdTitleHistoryToBo(adTitleHistoryList));
        return adPublishInputHistoryCache;
    }

    @Override
    public  List<AdTitleHistoryBo> getAdTitleHistory(Long creatorId, Long fbConnectionObjectId)
    {
        List<AdTitleHistory> adTitleHistoryList = adTitleHistoryRdbServices.findByCreatorIdAndConnectionObjectId(creatorId, fbConnectionObjectId);
        return tranferAdTitleHistoryToBo(adTitleHistoryList);
    }

    private List<AdTitleHistoryBo> tranferAdTitleHistoryToBo(List<AdTitleHistory> adTitleHistoryList) {
        if (CollectionUtils.isEmpty(adTitleHistoryList)) {
            return null;
        }
        List<AdTitleHistoryBo> boList = new ArrayList<>();
        AdTitleHistoryBo bo;
        for (AdTitleHistory adTitleHistory : adTitleHistoryList) {
            bo = new AdTitleHistoryBo();
            bo.setId(adTitleHistory.getId());
            bo.setTitle(adTitleHistory.getTitle());
            boList.add(bo);
        }
        return boList;
    }

    @Override
    public void saveInputAdTitleHistory(Long orgId, ConnectionObjectVo connectionObjectVo, String adTitle) {
        AdTitleHistory history = adTitleHistoryRdbServices.isExistedAdTitleHistory(orgId, connectionObjectVo.getFbConnectionObjectId(), adTitle);
        if (null == history) {
            AdTitleHistory obj = new AdTitleHistory();
            obj.setCreatorId(orgId);
            obj.setTitle(adTitle);
            obj.setConnectionObjectId(connectionObjectVo.getFbConnectionObjectId());
            obj.setConnectionObjectName(connectionObjectVo.getName());
            adTitleHistoryRdbServices.saveAdTitleHistory(obj);
        } else {
            logger.info("数据库已存在该标题记录：" + adTitle);
        }
    }
}
